<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      :disabled="isInput"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="合同编号" prop="htid">
        <el-input v-model="formData.htid" placeholder="请输入合同编号" />
      </el-form-item>
      <el-form-item label="合同名称" prop="htmc">
        <el-input v-model="formData.htmc" placeholder="请输入合同名称" />
      </el-form-item>
      <el-form-item label="合同类型" prop="htlx">
        <el-select v-model="formData.htlx" placeholder="请选择合同类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_HTLX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同类别" prop="htlb">
        <el-select v-model="formData.htlb" placeholder="请选择合同类别">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_HTLB)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二维码/条形码" prop="ewm">
        <UploadImg v-model="formData.ewm" />
      </el-form-item>
      <el-form-item label="合同金额" prop="htje">
        <el-input v-model="formData.htje" placeholder="请输入合同金额" />
      </el-form-item>
      <el-form-item label="服务开始时间" prop="kssj">
        <el-date-picker
          v-model="formData.kssj"
          type="date"
          value-format="x"
          placeholder="选择服务开始时间"
        />
      </el-form-item>
      <el-form-item label="服务结束时间" prop="jssj">
        <el-date-picker
          v-model="formData.jssj"
          type="date"
          value-format="x"
          placeholder="选择服务结束时间"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="xmmc">
        <el-input v-model="formData.xmmc" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="部门" prop="bm">
        <el-input v-model="formData.bm" placeholder="请输入部门" />
      </el-form-item>
      <el-form-item label="负责人" prop="fzr">
        <el-input v-model="formData.fzr" placeholder="请输入负责人" />
      </el-form-item>
      <el-form-item label="合同执行方式" prop="zxfs">
        <el-input v-model="formData.zxfs" placeholder="请输入合同执行方式" />
      </el-form-item>
      <el-form-item label="合同主要内容" prop="zynr">
       
        <Editor v-model="formData.zynr" height="150px" />
      </el-form-item>
      <el-form-item label="付款方式" prop="fkfs">
        <el-select v-model="formData.fkfs" placeholder="请选择付款方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_FKFS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户单位" prop="khdw">
        <el-input v-model="formData.khdw" placeholder="请输入客户单位" />
      </el-form-item>
      <el-form-item label="客户联系人" prop="khlxr">
        <el-input v-model="formData.khlxr" placeholder="请输入客户联系人" />
      </el-form-item>
      <el-form-item label="合同状态" prop="htzt">
        <el-select v-model="formData.htzt" placeholder="请选择合同状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_HTZT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="bz">
        <el-input v-model="formData.bz" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="签订时间" prop="qdsj">
        <el-date-picker
          v-model="formData.qdsj"
          type="date"
          value-format="x"
          placeholder="选择签订时间"
        />
      </el-form-item>
      <el-form-item label="线号" prop="xid">
        <el-input v-model="formData.xid" placeholder="请输入线号" />
      </el-form-item>
      <el-form-item label="A端地址" prop="jrdd">
        <el-input v-model="formData.jrdd" placeholder="请输入A端地址" />
      </el-form-item>
      <el-form-item label="Z端地址" prop="scdd">
        <el-input v-model="formData.scdd" placeholder="请输入Z端地址" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button v-show="ischeck" @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import * as BaseInfoApi from '@/api/cont/base'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  htid: undefined,
  htmc: undefined,
  htlx: undefined,
  htlb: undefined,
  ewm: undefined,
  htje: undefined,
  kssj: undefined,
  jssj: undefined,
  xmmc: undefined,
  bm: undefined,
  fzr: undefined,
  zxfs: undefined,
  zynr: " ",
  fkfs: undefined,
  khdw: undefined,
  khlxr: undefined,
  htzt: undefined,
  bz: undefined,
  qdsj: undefined,
  xid: undefined,
  jrdd: undefined,
  scdd: undefined
})
const formRules = reactive({
  htid: [{ required: true, message: '合同编号不能为空', trigger: 'blur' }]
})
const isInput = ref(false) //控制表单是否可以编辑
const ischeck = ref(true)
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  if(type === 'check'){
    isInput.value = true
    ischeck.value = false
  }
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseInfoApi.getBaseInfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BaseInfoApi.BaseInfoVO
    if (formType.value === 'create') {
      await BaseInfoApi.createBaseInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseInfoApi.updateBaseInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    htid: undefined,
    htmc: undefined,
    htlx: undefined,
    htlb: undefined,
    ewm: undefined,
    htje: undefined,
    kssj: undefined,
    jssj: undefined,
    xmmc: undefined,
    bm: undefined,
    fzr: undefined,
    zxfs: undefined,
    zynr: " ",
    fkfs: undefined,
    khdw: undefined,
    khlxr: undefined,
    htzt: undefined,
    bz: undefined,
    qdsj: undefined,
    xid: undefined,
    jrdd: undefined,
    scdd: undefined
  }
  formRef.value?.resetFields()
}
</script>